<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>编辑商家个人资料</title>
    <link rel="stylesheet" href="../static/css/bootstrap.css">
    <script src="../static/js/jquery.js"></script>
    <script src="../static/js/bootstrap.js"></script>
    <style>
        #myFile {
            opacity: 0;
        }
        h3 {
            color: #0084ff;
            font-weight: bold;
        }

        .title {
            font-size: 18px;
            border-bottom: 1px solid #0084ff;
            margin-top: 20px;
            padding-bottom: 5px;
        }
        img {
            width: 120px;
            height: 120px;
            border: 1px solid #f7f7f7;
            margin-top: 10px;
        }
        #upload-label{
            text-align: center;
            background-color: #0084ff;
            color: #fff;
            display: block;
            width: 120px;
            height: 35px;
            line-height: 35px;
        }
        .input-group {
            margin-top: 10px;
        }
        .input-group input {
            margin-left: 20px;
            padding-left: 10px;
            border: 0px;
        }
        .input-group button {
            margin-left: 20px;
        }

        .edit label {
            font-size: 12px;
            font-weight: lighter;
        }
        .edit-btn {
            width: 50px;
            height: 25px;
            background-size: 18px 18px;
            background-image: url("../static/images/edit.png");
            background-repeat: no-repeat;
            margin-left: 20px;
            cursor: pointer;
        }
        .edit-btn label {
            color: #0084ff;
            padding-left: 17px;
        }
    </style>
</head>
<body>
    <div class="container">
        <h3>个人资料</h3>

        <div class="edit">
            <div class="title">修改个人资料</div>
            <form id="info" action="#" method="post" enctype="multipart/form-data" class="form-horizontal">
                <div class="head">
                    <div>
                        <img id="head-img" src="../static/images/list-head.png" alt="默认头像">
                        <input name="avatarFile" type="file" id="myFile">
                        <label id="upload-label" for="myFile">上传头像</label>
                    </div>
                </div>
                <div class="input-group">
                    <label for="">用户名：</label>
                    <input name="real_name" type="text" class="name" readonly="readonly">
                </div>
                <div class="input-group">
                    <label for="">昵&nbsp;&nbsp;&nbsp;&nbsp;称：</label>
                    <input name="nickname" type="text" class="nickname" readonly="readonly">
                    <span class="edit-btn">
                        <label for="">修改</label>
                    </span>
                </div>
                <div class="input-group">
                    <label for="">手&nbsp;&nbsp;&nbsp;&nbsp;机：</label>
                    <input name="phone" type="text" class="phone" readonly="readonly">
                </div>
                <div class="input-group">
                    <label for="">邮&nbsp;&nbsp;&nbsp;&nbsp;箱：</label>
                    <input name="email" type="text" class="email" readonly="readonly">
                    <span class="edit-btn">
                        <label for="">修改</label>
                    </span>
                </div>
                <div class="input-group">
                    <button class="submit-info btn btn-primary">提交</button>
                    <button class="btn btn-default">取消</button>
                </div>
            </form>

        </div>
    </div>
</body>
</html>
<script>

    var curWwwPath = window.location.href;
    var pathName = window.location.pathname;
    var ctx = curWwwPath.substring(0,curWwwPath.indexOf(pathName));

    $(document).ready(function () {
        loginSuccess();

        previewHead();
        $(".edit-btn").click(function () {
            $(this).prev("input").removeAttr("readonly");
            $(this).prev("input").css({"border": "1px solid #0084ff"})
        })

        // 点击按钮上传资料
        $(".submit-info").click(function () {
            uploadInfo();
            console.log('更新成功')
            return false;
        })

    })

    // 加载用户的信息
    function loadHousekeeperInfo() {
        var url = ctx + "/hk/getHKPerson";
        $.ajax({
            "url": url,
            "dataType": "json",
            "type": "Post",
            "success": function (json) {
                if (json.state == 200) {
                    $(".name").val(json.data.hkName);
                    $(".nickname").val(json.data.hkNickname);
                    $(".phone").val(json.data.hkPhone);
                    $(".email").val(json.data.hkEmail);
                    $("#head-img").attr("src", "../" + json.data.hkHeadphoto);
                } else if (json.state == 407) {
                    alert(json.message);
                }
            }
        })
    }

    // 更新信息
    function uploadInfo() {
        var formData = new FormData($("#info")[0]);
        var url = ctx + "/hk/updateHousekeeperInfo";
        $.ajax({
            "url": url,
            "data": formData,
            "dataType": "json",
            "type": "Post",
            "contentType": false,
            "processData": false,
            "success": function (json) {
                if (json.state == 200) {
                    alert("个人信息修改成功");
                    window.location.href = "/hk/toInfoHk"
                } else if (json.state == 408) {
                    alert("个人信息修改失败");
                } else {
                    alert("用户未登录...")
                    top.location.href = "/user/toLogin";
                }
            }
        })
    }


    // 用户头像的展示
    function previewHead() {
        var file = document.getElementById("myFile");
        file.onchange = function(){  // 文本框内容改变时触发
            var files = this.files; // 获取文件的数量
            for(var i=0;i<files.length;i++){
                readers(files[i])
            }
        }
        function readers(fil){
            var reader = new FileReader();  // 异步读取存储在用户计算机上的文件
            reader.readAsDataURL(fil); // 开始读取指定的Blob对象或File对象中的内容
            reader.onload = function(){
                document.getElementById("head-img").src = reader.result;  // 添加图片到指定容器中
            };
        }
    }

    // 登录成功
    function loginSuccess() {
        $(".log-out").css({"display": "none"});
        $.ajax({
            "url": ctx + "/user/loginSuccess",
            "type": "Post",
            "success": function (json) {
                if (json.state == 200) {
                    $(".log-out").css({"display": "block"});
                    $(".log-in").css({"display": "none"});
                    $(".register").css({"display": "none"});

                    loadHousekeeperInfo();

                }else {
                    alert("用户未登录");
                    top.location.href = "/user/toLogin";
                }
            }
        })
    }
</script>